<?php
namespace app\admin\controller\station;

use app\admin\BaseController;
use app\admin\model\ChargePileCost;
use think\facade\Db;
use think\facade\Log;
use think\App;
use think\facade\View;
use think\facade\Request;

class BillingModel extends BaseController
{
    /**
     * 列表页面
     * @return string
     * @throws \think\db\exception\DataNotFoundException
     * @throws \think\db\exception\DbException
     * @throws \think\db\exception\ModelNotFoundException
     */
    public function index()
    {
        return View::fetch('billing/index');
    }

    /**
     * 获取列表
     * @return $this
     * @throws \think\db\exception\DataNotFoundException
     * @throws \think\db\exception\DbException
     * @throws \think\db\exception\ModelNotFoundException
     */
    public function list(){
        [$page,$limit] = $this->getPage();
        $getData = Request::post();
        $chargePileCostModel = new ChargePileCost();
        $backData = $chargePileCostModel->getList($getData,$page,$limit);
        $backData['code'] = 0;
        $backData['msg'] = '获取成功';
        return json()->data($backData);
    }

    /**
     * 添加表单
     * @return string
     * @throws \think\db\exception\DataNotFoundException
     * @throws \think\db\exception\DbException
     * @throws \think\db\exception\ModelNotFoundException
     */
    function addForm(){
        $getInfo = $this->getTitleList();
        $getTitle = $getInfo['title_list'];
        $modelList = $getInfo['model_list'];
        $getList = [];
        for($i = 1;$i<=48;$i++){
            $getList[] = [
                'title'=>$getTitle[$i],
                'name'=>'model-'.$i,
                'value'=>'01'
            ];
        }
        View::assign('getList', $getList);
        View::assign('modelList', $modelList);
        return View::fetch('billing/add');
    }

    /**
     * 获取标题
     * @return array
     */
    function getTitleList(){
        $titleList =  [
            1=>'0点-0点30分',
            2=>'0点30分-1点',
            3=>'1点-1点30分',
            4=>'1点30分-2点',
            5=>'2点-2点30分',
            6=>'2点30分-3点',
            7=>'3点-3点30分',
            8=>'3点30分-4点',
            9=>'4点-4点30分',
            10=>'4点30分-5点',
            11=>'5点-5点30分',
            12=>'5点30分-6点',
            13=>'6点-6点30分',
            14=>'6点30分-7点',
            15=>'7点-7点30分',
            16=>'7点30分-8点',
            17=>'8点-8点30分',
            18=>'8点30分-9点',
            19=>'9点-9点30分',
            20=>'9点30分-10点',
            21=>'10点-10点30分',
            22=>'10点30分-11点',
            23=>'11点-11点30分',
            24=>'11点30分-12点',
            25=>'12点-12点30分',
            26=>'12点30分-13点',
            27=>'13点-13点30分',
            28=>'13点30分-14点',
            29=>'14点-14点30分',
            30=>'14点30分-15点',
            31=>'15点-15点30分',
            32=>'15点30分-16点',
            33=>'16点-16点30分',
            34=>'16点30分-17点',
            35=>'17点-17点30分',
            36=>'17点30分-18点',
            37=>'18点-18点30分',
            38=>'18点30分-19点',
            39=>'19点-19点30分',
            40=>'19点30分-20点',
            41=>'20点-20点30分',
            42=>'20点30分-21点',
            43=>'21点-21点30分',
            44=>'21点30分-22点',
            45=>'22点-22点30分',
            46=>'22点30分-23点',
            47=>'23点-23点30分',
            48=>'23点30分-24点',
        ];
        $modeList = [
            ['title'=>'尖费率','value'=>'00'],
            ['title'=>'峰费率','value'=>'01'],
            ['title'=>'平费率','value'=>'02'],
            ['title'=>'谷费率','value'=>'03'],
        ];
        $nameList = ['00'=>'尖费率', '01'=>'峰费率', '02'=>'平费率', '03'=>'谷费率',];
        return ['title_list'=>$titleList,'model_list'=>$modeList,'name_list'=>$nameList];
    }

    /**
     * 添加站点
     * @return $this
     * @throws \think\db\exception\DataNotFoundException
     * @throws \think\db\exception\DbException
     * @throws \think\db\exception\ModelNotFoundException
     */
    function addData(){
        $getData = Request::post();
        if(!$getData['title']) return $this->fail('请输入计费模型名称');
        if(!$getData['billing_mode']) return $this->fail('请输入计费模型编号');
        $chargePileCostModel = new ChargePileCost();
        $costInfo = $chargePileCostModel->getInfoByWhere(['billing_mode'=>$getData['billing_mode']]);
        if($costInfo) return $this->fail('当前计费模型编号已经存在');
        //设置各个时段收费费率
        $getInfo = $this->getTitleList();
        $getTitle = $getInfo['title_list'];
        $nameList = $getInfo['name_list'];
        $getList = [];
        for($i = 1;$i<=48;$i++){
            $getList[] = [
                'title'=>$getTitle[$i],
                'name'=>'model-'.$i,
                'value'=>$getData['model-'.$i],
                'value_name'=>$nameList[$getData['model-'.$i]]
            ];
        }
        $getData['cost_value'] = $getList;

        $result = $chargePileCostModel->addData($getData);
        if($result) return $this->success('添加成功');
        return $this->fail('添加失败');
    }

    /**
     * 编辑站点
     * @return string
     * @throws \think\db\exception\DataNotFoundException
     * @throws \think\db\exception\DbException
     * @throws \think\db\exception\ModelNotFoundException
     */
    function editForm(){
        $id = Request::get('id');
        $chargePileCostModel = new ChargePileCost();
        $info = $chargePileCostModel->getInfoById($id);
        $infoStatus = 0;
        $getInfo = $this->getTitleList();
        $modelList = $getInfo['model_list'];
        if($info)$infoStatus = 1;
        View::assign('modelList', $modelList);
        View::assign('info', $info);
        View::assign('infoStatus', $infoStatus);
        return View::fetch('billing/edit');
    }

    /**
     * 修改站点
     * @return $this
     * @throws \think\db\exception\DataNotFoundException
     * @throws \think\db\exception\DbException
     * @throws \think\db\exception\ModelNotFoundException
     */
    function updateData(){
        $getData = Request::post();
        if(!$getData['title']) return $this->fail('请输入计费模型名称');
        if(!$getData['billing_mode']) return $this->fail('请输入计费模型编号');
        $chargePileCostModel = new ChargePileCost();
        $costInfo = $chargePileCostModel->getInfoByWhere(['billing_mode'=>$getData['billing_mode']]);
        if($costInfo['id'] != $getData['id']) return $this->fail('当前计费模型编号已经存在');
        //设置各个时段收费费率
        $getInfo = $this->getTitleList();
        $getTitle = $getInfo['title_list'];
        $nameList = $getInfo['name_list'];
        $getList = [];
        for($i = 1;$i<=48;$i++){
            $getList[] = [
                'title'=>$getTitle[$i],
                'name'=>'model-'.$i,
                'value'=>$getData['model-'.$i],
                'value_name'=>$nameList[$getData['model-'.$i]]
            ];
        }
        $getData['cost_value'] = $getList;
        $result = $chargePileCostModel->editData($getData);
        if($result) return $this->success('修改成功');
        return $this->fail('未作任何修改');
    }

    /**
     * 删除站点
     * @return $this
     * @throws \think\db\exception\DataNotFoundException
     * @throws \think\db\exception\DbException
     * @throws \think\db\exception\ModelNotFoundException
     */
    function deleteData(){
        $id = Request::post('id');
        if(!$id) return $this->fail('请选择要删除的计费模型');
        $chargePileCostModel = new ChargePileCost();
        $result = $chargePileCostModel->delData($id);
        if($result['code'] == 0) return $this->success('删除成功');
        return $this->fail($result['msg']);
    }

    /**
     * 获取计费模型内容
     * @return $this|string
     * @throws \think\db\exception\DataNotFoundException
     * @throws \think\db\exception\DbException
     * @throws \think\db\exception\ModelNotFoundException
     */
    function getInfo(){
        $id = Request::get('id');
        if(!$id) return $this->fail('请选择要删除的计费模型');
        $chargePileCostModel = new ChargePileCost();
        $info = $chargePileCostModel->getInfoById($id);
        $infoStatus = 0;
        if($info)$infoStatus = 1;
        View::assign('info', $info);
        View::assign('infoStatus', $infoStatus);
        return View::fetch('billing/info');

    }

}


